using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class EventManager
{
    private Dictionary<string, UnityEvent> eventDict = new Dictionary<string, UnityEvent>();

    private static EventManager eventManager = new EventManager();

    public static EventManager GetInstance
    {
        get
        {
            return eventManager;
        }
    }

    public void StartListening(string eventName, UnityAction listener)
    {
        UnityEvent thisEvent = null;
        if (eventManager.eventDict.TryGetValue(eventName, out thisEvent)) {
            thisEvent.AddListener(listener);
        } else
        {
            thisEvent = new UnityEvent();
            thisEvent.AddListener(listener);
            eventManager.eventDict.Add(eventName, thisEvent);
        }
    }

    public void StopListening(string eventName, UnityAction listener)
    {
        if (eventManager == null) return;

        UnityEvent thisEvent = null;
        if (eventManager.eventDict.TryGetValue(eventName, out thisEvent))
        {
            thisEvent.RemoveListener(listener);
        }
    }

    public void TriggerEvent(string eventName)
    {
        UnityEvent thisEvent = null;
        if (eventManager.eventDict.TryGetValue(eventName, out thisEvent))
        {
            thisEvent.Invoke();
        }
    }
}